// lib/routes/app_router.dart
import 'package:flutter/material.dart';
import 'package:enjoy_plus_community/pages/home/components/notice_detail.dart';
import 'package:enjoy_plus_community/pages/house/components/building_list.dart';
import 'package:enjoy_plus_community/pages/house/components/house_detail.dart';
import 'package:enjoy_plus_community/pages/house/components/house_form.dart';
import 'package:enjoy_plus_community/pages/house/components/location_list.dart';
import 'package:enjoy_plus_community/pages/house/components/room_list.dart';
import 'package:enjoy_plus_community/pages/house/house_list.dart';
import 'package:enjoy_plus_community/pages/login/index.dart';
import 'package:enjoy_plus_community/pages/profile/index.dart';
import 'package:enjoy_plus_community/pages/tab_bar_page.dart';
import 'package:enjoy_plus_community/utils/token.dart';

class AppRouter {
  // 路由名称常量（避免硬编码）
  static const String home = '/';
  static const String login = '/login';
  static const String profile = '/profile';
  static const String houseList = '/house_list';
  static const String locationList = '/location_list';
  static const String buildingList = '/building_list';
  static const String roomList = '/room_list';
  static const String houseForm = '/house_form';
  static const String houseDetail = '/house_detail';
  static const String noticeDetail = '/detail';

  // 静态路由表（无需参数的路由）
  static Map<String, WidgetBuilder> get routes => {
        home: (context) => const TabBarPage(),
        noticeDetail: (context) => const NoticeDetail(),
        login: (context) => const LoginPage(),
      };

  // 动态路由生成（带参数或逻辑的路由）
  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    final tokenManager = TokenManager();
    final token = tokenManager.getToken() ?? '';

    // Token 验证逻辑
    if (token.isEmpty && settings.name != login) {
      return MaterialPageRoute(builder: (_) => const LoginPage());
    }

    // 根据路由名称返回对应页面
    switch (settings.name) {
      case profile:
        return MaterialPageRoute(
          builder: (_) => ProfilePage(userInfo: settings.arguments as Map),
        );
      case houseList:
        return MaterialPageRoute(builder: (_) => const HouseList());
      case locationList:
        return MaterialPageRoute(builder: (_) => const LocationList());
      case buildingList:
        final args = settings.arguments as Map;
        return MaterialPageRoute(
          builder: (_) => BuildingList(point: args['point']),
        );
      case roomList:
        final args = settings.arguments as Map;
        return MaterialPageRoute(
          builder: (_) => RoomList(
            point: args['point'],
            building: args['building'],
          ),
        );
      case houseForm:
        return MaterialPageRoute(
          builder: (_) => HouseForm(params: settings.arguments as Map),
        );
      case houseDetail:
        final args = settings.arguments as Map;
        return MaterialPageRoute(
          builder: (_) => HouseDetail(id: args['id']),
        );
      default:
        return MaterialPageRoute(builder: (_) => const TabBarPage());
    }
  }
}